package com.atguigu.gmall.activity.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/***
 * 秒杀订单延时队列，用来处理超时的订单
 */
@Configuration
public class SeckillOrderDelayRabbitConfig {

    /**
     * 延时交换机
     * @return
     */
    @Bean("seckillOrderDelayExchange")
    public Exchange seckillOrderDelayExchange(){
        return ExchangeBuilder.directExchange("seckill_order_delay_exchange").build();
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean("seckillOrderDeadExchange")
    public Exchange seckillOrderDeadExchange(){
        return ExchangeBuilder.directExchange("seckill_order_dead_exchange").build();
    }

    /**
     * 延时队列
     * @return
     */
    @Bean("seckillOrderDelayQueue")
    public Queue seckillOrderDelayQueue(){
        return QueueBuilder
                .durable("seckill_order_delay_queue")
                .withArgument("x-dead-letter-exchange","seckill_order_dead_exchange")
                .withArgument("x-dead-letter-routing-key","seckill.order.dead")
                .build();
    }

    /**
     * 死信队列
     * @return
     */
    @Bean("seckillOrderDeadQueue")
    public Queue seckillOrderDeadQueue(){
        return QueueBuilder.durable("seckill_order_dead_queue").build();
    }

    /**
     * 延时队列与延时交换绑定
     * @param seckillOrderDelayExchange
     * @param seckillOrderDelayQueue
     * @return
     */
    @Bean
    public Binding seckillOrderDelayBinding(@Qualifier("seckillOrderDelayExchange") Exchange seckillOrderDelayExchange,
                                @Qualifier("seckillOrderDelayQueue") Queue seckillOrderDelayQueue){
        return BindingBuilder.bind(seckillOrderDelayQueue).to(seckillOrderDelayExchange).with("seckill.order.delay").noargs();
    }

    /**
     * 死信队列与死信交换绑定
     * @param seckillOrderDeadExchange
     * @param seckillOrderDeadQueue
     * @return
     */
    @Bean
    public Binding seckillOrderDeadBinding(@Qualifier("seckillOrderDeadExchange") Exchange seckillOrderDeadExchange,
                               @Qualifier("seckillOrderDeadQueue") Queue seckillOrderDeadQueue){
        return BindingBuilder.bind(seckillOrderDeadQueue).to(seckillOrderDeadExchange).with("seckill.order.dead").noargs();
    }

}
